package com.yic.module.mes.service.process;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.mes.dal.redis.RedisKeyConstants;
import com.yic.module.mes.service.moroute.MoRouteService;
import com.yic.module.mes.service.processRoute.ProcessRouteService;
import com.yic.module.mes.service.processrouteinfo.ProcessRouteInfoService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.mes.controller.admin.process.vo.*;
import com.yic.module.mes.dal.dataobject.process.ProcessDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.mes.convert.process.ProcessConvert;
import com.yic.module.mes.dal.mysql.process.ProcessMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.ITEM_CODE_EXISTS;
import static com.yic.module.base.enums.ErrorCodeConstants.ITEM_NAME_EXISTS;
import static com.yic.module.mes.enums.ErrorCodeConstants.*;

/**
 * 工艺信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ProcessServiceImpl implements ProcessService {

    @Resource
    private ProcessMapper processMapper;
    @Resource
    private ProcessRouteInfoService processRouteInfoService;
    @Resource
    private MoRouteService moRouteService;

    @Override
    public Long createProcess(ProcessCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode());
        // 插入
        ProcessDO process = ProcessConvert.INSTANCE.convert(createReqVO);
        processMapper.insert(process);
        // 返回
        return process.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.PROCESS, key = "#updateReqVO.id")
    public void updateProcess(ProcessUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode());
        // 更新
        ProcessDO updateObj = ProcessConvert.INSTANCE.convert(updateReqVO);
        processMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.PROCESS, key = "#id")
    public void deleteProcess(Long id) {
        // 校验存在
        validateProcessExists(id);
        // 工艺如果被使用，不允许删除
        validateProcessUsed(id);
        // 删除
        processMapper.deleteById(id);
    }

    private void validateProcessUsed(Long id) {
        //被工艺路线使用 被工单工艺路线使用
        if (processRouteInfoService.checkUseProcess(id) || moRouteService.checkUseProcess(id)) {
            throw exception(PROCESS_CODE_USED);
        }
    }

    private void validateCreateOrUpdate(Long id, String name, String code){
        validateNameExists(id, name);
        validateCodeExist(id, code);
    }

    private void validateNameExists(Long id, String name) {
        ProcessDO process = processMapper.selectByName(name);
        if (ObjectUtil.isEmpty(process)){
            return;
        }
        if (id == null){
            throw exception(PROCESS_NAME_EXISTS);
        }
        if (!id.equals(process.getId())){
            throw exception(PROCESS_NAME_EXISTS);
        }
    }

    private void validateCodeExist(Long id, String code){
        ProcessDO process = processMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(process)){
            return;
        }
        if (id == null){
            throw exception(PROCESS_CODE_EXISTS);
        }
        if (!id.equals(process.getId())){
            throw exception(PROCESS_CODE_EXISTS);
        }
    }

    private void validateProcessExists(Long id) {
        if (processMapper.selectById(id) == null) {
            throw exception(PROCESS_NOT_EXISTS);
        }
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.PROCESS, key = "#id")
    public ProcessDO getProcess(Long id) {
        return processMapper.selectById(id);
    }

    @Override
    public List<ProcessDO> getProcessList(Collection<Long> ids) {
        return processMapper.selectBatchIds(ids);
    }

    @Override
    public List<ProcessDO> getProcessList(ProcessPageReqVO pageReqVO) {
        return processMapper.selectList(pageReqVO);
    }

    @Override
    public PageResult<ProcessDO> getProcessPage(ProcessPageReqVO pageReqVO) {
        return processMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProcessDO> getProcessList(ProcessExportReqVO exportReqVO) {
        return processMapper.selectList(exportReqVO);
    }

}
